package com.ruoyi.buss.task.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.buss.customer.mapper.DataCustomerInfoMapper;
import com.ruoyi.buss.customer.service.IDataCustomerInfoService;
import com.ruoyi.buss.domain.customer.rsp.ResCustinfo;
import com.ruoyi.buss.grid.cj.domain.DataCustomerInfoVO;
import com.ruoyi.buss.group.domain.DataCustomerGroup;
import com.ruoyi.buss.group.mapper.DataCustomerGroupMapper;
import com.ruoyi.buss.qmcy.domain.QmcyCustPersonalInfo;
import com.ruoyi.buss.system.service.IIusOrgInfoService;
import com.ruoyi.buss.task.domain.DataPlanTaskRelation;
import com.ruoyi.buss.task.domain.DataUserTaskRelation;
import com.ruoyi.buss.task.mapper.DataUserTaskRelationMapper;
import com.ruoyi.buss.task.service.IDataUserTaskRelationService;
import com.ruoyi.buss.utils.ListUtil;
import com.ruoyi.common.core.utils.DateUtils;

/**
 * 营销任务和客户关系Service业务层处理
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class DataUserTaskRelationServiceImpl implements IDataUserTaskRelationService 
{
    @Autowired
    private DataUserTaskRelationMapper dataUserTaskRelationMapper;
	@Autowired
	private IDataCustomerInfoService iDataCustomerInfoService;
	@Autowired
	private DataCustomerInfoMapper dataCustomerInfoMapper;
	@Autowired
	private IIusOrgInfoService iIusOrgInfoService;
	@Autowired
	private DataCustomerGroupMapper dataCustomerGroupMapper;
    /**
     * 查询营销任务和客户关系
     * 
     * @param id 营销任务和客户关系ID
     * @return 营销任务和客户关系
     */
    @Override
    public DataUserTaskRelation selectDataUserTaskRelationById(String id)
    {
        return dataUserTaskRelationMapper.selectDataUserTaskRelationById(id);
    }

    /**
     * 查询营销任务和客户关系列表
     * 
     * @param dataUserTaskRelation 营销任务和客户关系
     * @return 营销任务和客户关系
     */
    @Override
    public List<DataUserTaskRelation> selectDataUserTaskRelationList(DataUserTaskRelation dataUserTaskRelation)
    {
        return dataUserTaskRelationMapper.selectDataUserTaskRelationList(dataUserTaskRelation);
    }

    /**
     * 新增营销任务和客户关系
     * 
     * @param dataUserTaskRelation 营销任务和客户关系
     * @return 结果
     */
    @Override
    public int insertDataUserTaskRelation(DataUserTaskRelation dataUserTaskRelation)
    {
        dataUserTaskRelation.setCreateTime(DateUtils.getNowDate());
        return dataUserTaskRelationMapper.insertDataUserTaskRelation(dataUserTaskRelation);
    }

    /**
     * 修改营销任务和客户关系
     * 
     * @param dataUserTaskRelation 营销任务和客户关系
     * @return 结果
     */
    @Override
    public int updateDataUserTaskRelation(DataUserTaskRelation dataUserTaskRelation)
    {
        return dataUserTaskRelationMapper.updateDataUserTaskRelation(dataUserTaskRelation);
    }

    /**
     * 批量删除营销任务和客户关系
     * 
     * @param ids 需要删除的营销任务和客户关系ID
     * @return 结果
     */
    @Override
    public int deleteDataUserTaskRelationByIds(String[] ids)
    {
        return dataUserTaskRelationMapper.deleteDataUserTaskRelationByIds(ids);
    }

    /**
     * 删除营销任务和客户关系信息
     * 
     * @param id 营销任务和客户关系ID
     * @return 结果
     */
    @Override
    public int deleteDataUserTaskRelationById(String id)
    {
        return dataUserTaskRelationMapper.deleteDataUserTaskRelationById(id);
    }
    
    
	public List<DataCustomerInfoVO> listIdCardByTask(DataPlanTaskRelation dataPlanTaskRelation){
		List<String> idcardList = dataUserTaskRelationMapper.listIdCardByTask(dataPlanTaskRelation);
		List<DataCustomerInfoVO> resultList = this.getCustomersByIdCards(idcardList);
		if(!ListUtil.isNullOrEmpty(resultList)){
			for(DataCustomerInfoVO data:resultList){
				if(StringUtils.isNotEmpty(data.getIdCard()))
				{
					String idCardShow = iIusOrgInfoService.getIdCardShow(data.getIdCard());
					data.setIdCard(idCardShow);
				}
			}
		}
		return resultList;
	}
	
	public List<DataCustomerInfoVO> listCustomerByOrgUser(DataPlanTaskRelation dataPlanTaskRelation){

		List<String> idcardList = dataUserTaskRelationMapper.listByOrgUser(dataPlanTaskRelation);

		List<DataCustomerInfoVO> resultList = this.getCustomersByIdCards(idcardList);

		//返回头像
		for (DataCustomerInfoVO temp : resultList)
		{
			List<DataCustomerGroup> groupLabel = dataCustomerGroupMapper.listGroupByIdcard(temp.getIdCard());

			if (!ListUtil.isNullOrEmpty(groupLabel))
			{
				temp.setHeadPic(groupLabel.get(0).getGroupCode());
			}

			temp.setHeadPic(iDataCustomerInfoService.getHeadPic(temp.getIdCard(),temp.getSex()));
		}

		return resultList;
	}

	public List<DataCustomerInfoVO> getCustomersByIdCards(List<String> idCards)
	{
		List<DataCustomerInfoVO> resultList = new ArrayList<>();
		for(String idcard :idCards)
		{
			DataCustomerInfoVO infoVO = new DataCustomerInfoVO();
			ResCustinfo resCustinfo = new ResCustinfo();
			infoVO.setIdCard(idcard);
			List<ResCustinfo> resCustinfos = dataCustomerInfoMapper.getCustInfo(infoVO.getIdCard());
			if(!ListUtil.isNullOrEmpty(resCustinfos))
			{
				resCustinfo = resCustinfos.get(0);
				if(null!=resCustinfo)
				{
					BeanUtils.copyProperties(resCustinfo,infoVO);
				}
				resCustinfo.setHeadPic(iDataCustomerInfoService.getHeadPic(resCustinfo.getIdCard(),resCustinfo.getSex()));
			}
			infoVO.setId(infoVO.getIdCard());
			//查询客户产品信息
			List<QmcyCustPersonalInfo> proLabel = dataCustomerInfoMapper.queryProductLabelList(idcard);
			infoVO.setProductLabel(proLabel);
			resultList.add(infoVO);
		}
		return resultList;
	}
}
